package com.hazardSoft.eStore.bussines;

import java.util.List;

import javax.ejb.EJBException;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Bill;
import com.hazardSoft.eStore.entities.Inventory;
import com.hazardSoft.eStore.entities.Offer;
import com.hazardSoft.eStore.entities.Office;
import com.hazardSoft.eStore.entities.Product;
import com.hazardSoft.eStore.entities.Sale;
import com.hazardSoft.eStore.utils.Dates;
import com.hazardSoft.eStore.utils.PathConstants;
import com.hazardSoft.eStore.utils.QueryConstants;

public class SalesBO {

	private static final String CLASS_NAME = PathConstants.BUSSINES_PATH+SalesBO.class.getSimpleName();
	
	public Sale insertSale(Sale sale, EntityManager entityManager) throws SystemException {
		
		if (sale == null) {
//			throw new SystemException(new Exception("null sale"), CLASS_NAME, "insertSale");
			throw new EJBException("insertSale");
		}
		if (sale.getBill() == null) {
//			throw new SystemException(new Exception("null sale.bill"), CLASS_NAME, "insertSale");
			throw new EJBException("insertSale");
		}
		if (sale.getProduct() == null) {
//			throw new SystemException(new Exception("null sale.product"), CLASS_NAME, "insertSale");
			throw new EJBException("insertSale");
		}
		if (sale.getQuantity() == null) {
//			throw new SystemException(new Exception("null sale.quantity"), CLASS_NAME, "insertSale");
			throw new EJBException("insertSale");
		}
		
		InventoryBO inventoryBO = new InventoryBO();
		
		Inventory inventory = new Inventory();
		inventory.setProduct(sale.getProduct());
		
		if (!(sale.getBill().getSeller() == null)) {
			inventory.setOffice(sale.getBill().getSeller().getOffice());
			
		} else {
			//datos officina principal
			Office office = new Office();
			office.setId(1);
			
			inventory.setOffice(office);
		}
		
		inventory = inventoryBO.searchInventory(inventory, entityManager);
		
		if (!(inventory == null)) {
			if (!(inventory.getOffice().getId().equals(1))) {
				Integer quantity = inventory.getQuantity() - sale.getQuantity();
				
				if (quantity >= 0) {
					inventory.setQuantity(quantity);
				}
			}			
			Double cost = this.CalculateCost(sale.getProduct(), sale.getQuantity(), entityManager);
			sale.setCost(cost);

			inventoryBO.updateInventory(inventory, entityManager);
			if (sale.getId() == null) {
				sale.setId(0);
			}
			entityManager.persist(sale);
			entityManager.flush();
			return searchSale(sale, entityManager);
		}
		return null;
	}

	
	public List<Sale> listSalesByBill(Bill bill, EntityManager entityManager) throws SystemException {
		 if (bill == null) {
			 throw new SystemException(new Exception("null bill"), CLASS_NAME, "listSalesByBill");
		 }
		 
		 Query query = entityManager.createNamedQuery(QueryConstants.QUERY_LIST_SALES_BY_BILL);
		 query.setParameter(1, bill);
		
		 return query.getResultList();
	}

	private Double CalculateCost(Product product, Integer quantity, EntityManager entityManager) throws SystemException {
		 Double cost = product.getCost();
		 
		 Offer offer = product.getOffer();
		 if (!(offer == null)) {
			 if (offer.getOffertype().getId().equals(1)) {
				Integer tempQuantity = offer.getQuantity() - quantity;
				if (tempQuantity >= 0) {
					Double discount = cost * (offer.getPorcentage()/100.0);
					cost = (cost - discount) * quantity;
					
					offer.setQuantity(tempQuantity);
					OfferBO offerBO = new OfferBO();
					offerBO.updateOffer(offer, entityManager);
					
				} else {
					tempQuantity *= -1;
										
					Double discount = cost * (offer.getPorcentage()/100.0);
					cost = ((cost - discount) * (quantity - tempQuantity)) + (cost * tempQuantity);
					offer.setQuantity(0);
					OfferBO offerBO = new OfferBO();
					offerBO.updateOffer(offer, entityManager);
				}
			 } else {
				 if (Dates.compareDates(offer.getEnddate())) {
					 Double discount = cost * (offer.getPorcentage()/100.0);
					 cost = (cost - discount) * quantity;
				 } else {
					 cost *= quantity;
				 }
			 }
		 } else {
			 cost *= quantity;
		 }
		 return cost;
	}
	
	public Sale searchSale(Sale sale, EntityManager entityManager) throws SystemException{
		if (sale == null) {
			throw new SystemException(new Exception("null sale"), CLASS_NAME, "searchSale");
		}
		if (sale.getProduct() == null) {
			throw new SystemException(new Exception("null sale.product"), CLASS_NAME, "searchSale");
		}
		if (sale.getBill() == null) {
			throw new SystemException(new Exception("null sale.bill"), CLASS_NAME, "searchSale");
		}
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_SALE);
		query.setParameter(1, sale.getProduct());
		query.setParameter(2, sale.getBill());
		
		List<Sale> sales = query.getResultList();
		
		if (sales.size() > 0) {
			return sales.get(0);
		}
		
		return null;
	}
}
